package com.py.androidUtils.UI;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.py.androidUtils.UI.adapter.GroupUserAdapter;
import com.py.androidUtils.UI.model.UserInfoView;
import com.py.androidUtils.UI.view.ShowEmptyChatRecordView;
import com.py.androidUtils.UI.view.ShowEmptyChatRecordView.EmptyChatClickListener;
import com.py.androidUtils.base.BaseActivity;
import com.py.androidUtils.utils.ToastUtil;
import com.py.androidUtils.view.NoScrollGridView;
import com.readyidu.app.R;

/**
 * @说明 聊天信息
 * @作者 LY
 * @文件 GroupInfoActivity.java
 * @时间 2015年7月21日 下午10:12:42
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public class GroupInfoActivity extends BaseActivity implements OnClickListener {
	private NoScrollGridView gridView;
	private RelativeLayout rlGroupName;
	private TextView groupName;
	private CheckBox messageDisturb;
	private RelativeLayout groupNikeName;
	private TextView nickName;
	private TextView emptyMessage;
	private TextView report;
	private TextView delectQuit;

	private GroupUserAdapter groupUserAdapter;
	private LinearLayout line_bg;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_group_info);

		gridView = (NoScrollGridView) this.findViewById(R.id.gridView);
		groupUserAdapter = new GroupUserAdapter(GroupInfoActivity.this);
		gridView.setAdapter(groupUserAdapter);

		rlGroupName = (RelativeLayout) this.findViewById(R.id.rlGroupName);
		groupName = (TextView) this.findViewById(R.id.groupName);
		messageDisturb = (CheckBox) this.findViewById(R.id.messageDisturb);
		groupNikeName = (RelativeLayout) this.findViewById(R.id.groupNikeName);
		nickName = (TextView) this.findViewById(R.id.nickName);
		emptyMessage = (TextView) this.findViewById(R.id.emptyMessage);
		report = (TextView) this.findViewById(R.id.report);
		delectQuit = (TextView) this.findViewById(R.id.delectQuit);
		line_bg = (LinearLayout) this.findViewById(R.id.line_bg);

		getTitleActionBar().setTitleBar("聊天信息（3）人");
		getTitleActionBar().setImgLeft(R.drawable.icon_back, new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finishactivity(GroupInfoActivity.this);
			}
		});
		rlGroupName.setOnClickListener(this);
		groupNikeName.setOnClickListener(this);
		emptyMessage.setOnClickListener(this);
		report.setOnClickListener(this);
		delectQuit.setOnClickListener(this);

		List<UserInfoView> list = new ArrayList<UserInfoView>();
		UserInfoView infoView = new UserInfoView();
		infoView.setImg("http://img5.imgtn.bdimg.com/it/u=3638412718,2736228005&fm=21&gp=0.jpg");
		infoView.setName("测试a");
		infoView.setType(0);
		list.add(infoView);

		UserInfoView infoView1 = new UserInfoView();
		infoView1.setImg("http://img1.imgtn.bdimg.com/it/u=457904069,2178974804&fm=21&gp=0.jpg");
		infoView1.setName("测试b");
		infoView1.setType(0);
		list.add(infoView1);

		UserInfoView infoView2 = new UserInfoView();
		infoView2.setImg("http://www.qqzhi.com/uploadpic/2014-09-05/160840259.jpg");
		infoView2.setName("测试c");
		infoView2.setType(0);
		list.add(infoView2);

		UserInfoView infoView3 = new UserInfoView();
		infoView3.setType(1);
		list.add(infoView3);

		UserInfoView infoView4 = new UserInfoView();
		infoView4.setType(2);
		list.add(infoView4);

		groupUserAdapter.setList(list);

		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				UserInfoView infoView = (UserInfoView) groupUserAdapter.getListData().get(position);
				if (!groupUserAdapter.isDelete) {
					if (infoView.getType() == 0) {
						ToastUtil.showToast(getApplicationContext(), infoView.getName());
					} else if (infoView.getType() == 1) {
						ToastUtil.showToast(getApplicationContext(), "添加成员");
					} else if (infoView.getType() == 2) {
						groupUserAdapter.setDelete();
					}
				} else {
					groupUserAdapter.setDelete();
				}
			}
		});
		line_bg.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (groupUserAdapter.isDelete) {
					groupUserAdapter.setDelete();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		Bundle extras;
		switch (v.getId()) {
		case R.id.rlGroupName:
			ToastUtil.showToast(getApplicationContext(), "修改群名称");
			extras = new Bundle();
			extras.putString("name", groupName.getText().toString());
			launcherActivityForResult(GroupInfoActivity.this, GroupNameActivity.class, extras, 1);
			break;
		case R.id.groupNikeName:
			ToastUtil.showToast(getApplicationContext(), "修改昵称");
			extras = new Bundle();
			extras.putString("name", nickName.getText().toString());
			launcherActivityForResult(GroupInfoActivity.this, GroupNickNameActivity.class, extras, 2);
			break;
		case R.id.emptyMessage:
			ShowEmptyChatRecordView chatRecordView = new ShowEmptyChatRecordView(GroupInfoActivity.this);
			chatRecordView.showView(emptyMessage);
			chatRecordView.setEmptyChatClickListener(new EmptyChatClickListener() {
				@Override
				public void emptyChat() {
					ToastUtil.showToast(getApplicationContext(), "清空消息记录");
				}
			});
			break;
		case R.id.report:
			ToastUtil.showToast(getApplicationContext(), "举报");
			launcherActivity(GroupInfoActivity.this, ReportActivity.class, null);
			break;
		case R.id.delectQuit:
			ToastUtil.showToast(getApplicationContext(), "删除并退出");
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if (arg1 == RESULT_OK) {
			switch (arg0) {
			case 1:
				groupName.setText(arg2.getStringExtra("name"));
				break;
			case 2:
				nickName.setText(arg2.getStringExtra("name"));
				break;
			}
		}
	}
}
